package cn.tedu.truetaste.controller;

import cn.tedu.truetaste.pojo.dto.UserAddNewDTO;
import cn.tedu.truetaste.pojo.dto.UserUpdateDTO;
import cn.tedu.truetaste.pojo.vo.UserListItemVO;
import cn.tedu.truetaste.pojo.vo.UserStandardVO;
import cn.tedu.truetaste.service.IUserService;
import cn.tedu.truetaste.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理用户数据相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Api(tags = "用户管理模块")
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    public UserController() {
        log.info("创建控制器对象：UserController");
    }

    // http://localhost:9989/user/add-new
    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(UserAddNewDTO userAddNewDTO) {
        log.debug("开始处理【添加用户】的请求，参数：{}", userAddNewDTO);
        userService.addNew(userAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9989/user/9527/delete
    @ApiOperation("删除用户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【删除用户】的请求，参数：{}", id);
        userService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9989/user/9527/update
    @ApiOperation("修改用户详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, UserUpdateDTO userUpdateDTO) {
        log.debug("开始处理【修改用户详情】的请求，参数ID：{}, 新数据：{}", id, userUpdateDTO);
        userService.updateInfoById(id, userUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9989/user/9527
    @ApiOperation("根据openId查询用户详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/wx/{openId:[0-9]+}")
    public JsonResult<UserStandardVO> getStandardByOpenId(@PathVariable String openId) {
        log.debug("开始处理【根据openId查询用户详情】的请求，参数：{}", openId);
        UserStandardVO userStandardVO = userService.getStandardByOpenId(openId);
        return JsonResult.ok(userStandardVO);
    }

    // http://localhost:9989/user/9527
    @ApiOperation("根据id查询用户详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<UserStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询用户详情】的请求，参数：{}", id);
        UserStandardVO userStandardVO = userService.getStandardById(id);
        return JsonResult.ok(userStandardVO);
    }

    // http://localhost:9989/user
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<UserListItemVO>> list() {
        log.debug("开始处理【查询用户列表】的请求，无参数");
        List<UserListItemVO> list = userService.list();
        return JsonResult.ok(list);
    }

}
